package top.yangbuyi.controller.system;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.yangbuyi.acpect.annotaction.Log;
import top.yangbuyi.acpect.annotaction.ULogAnnotation;
import top.yangbuyi.acpect.enums.BusinessType;
import top.yangbuyi.dto.DictTypeDto;
import top.yangbuyi.service.impl.DictTypeService;
import top.yangbuyi.utils.ShiroSecurityUtils;
import top.yangbuyi.vo.AjaxResult;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @description: 杨不易网站:www.yangbuyi.top
 * @program: yangbuyiopenHis2020
 * @ClassName: DictTypeController
 * @create: 2020-08-29 19:45
 * @author: yangbuyi
 * @since： JDK1.8
 * @DictTypeController: 字典管理$ 类字典管理
 **/

@RestController
@Slf4j
@RequestMapping("system/dict/type")
public class DictTypeController {

	  @Autowired
	  private DictTypeService dictTypeService;

	  /**
	   * @功能描述: 字典类型分页查询
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @GetMapping("listForPage")
	  @ULogAnnotation(title = "字典查询", action = "字典类型分页查询")
	  @Log(title = "字典类型分页查询", businessType = BusinessType.SELECT)
	  public AjaxResult listForPage(DictTypeDto dictTypeDto) {
			return AjaxResult.success(dictTypeService.listPage(dictTypeDto));
	  }


	  /**
	   * @功能描述: 字典类型添加
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @PostMapping("addDictType")
	  @ULogAnnotation(title = "字典查询", action = "字典类型分页查询")
	  @Log(title = "字典类型添加", businessType = BusinessType.INSERT)
	  public AjaxResult addDictType(@Validated DictTypeDto dictTypeDto) {
			// 	校验数据是否存在
			if (this.dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
				  return AjaxResult.fail("新增字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在!");
			}
			// 设置添加人员
			dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.dictTypeService.insert(dictTypeDto));
	  }

	  /**
	   * @功能描述: 字典类型修改
	   * @Author: TeouBle
	   * @Date: 2020/8/29 19:50
	   */
	  @PutMapping("updateDictType")
	  @Log(title = "字典类型修改", businessType = BusinessType.UPDATE)
	  public AjaxResult updateDictType(@Validated DictTypeDto dictTypeDto) {
			// 	校验数据是否存在
			if (this.dictTypeService.checkDictTypeUnique(dictTypeDto.getDictId(), dictTypeDto.getDictType())) {
				  return AjaxResult.fail("修改字典【" + dictTypeDto.getDictName() + "】失败，字典类型已存在!");
			}
			// 设置修改人员
			dictTypeDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
			return AjaxResult.toAjax(this.dictTypeService.update(dictTypeDto));
	  }

	  /**
	   * @功能描述: 根据ID查询一个字典信息
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @GetMapping("getOne/{dictid}")
	  @Log(title = "根据ID查询一个字典信息", businessType = BusinessType.SELECT)
	  public AjaxResult getDictOneById(@NotNull(message = "类型字典ID不能为空") @PathVariable @Validated Long dictid) {
			return AjaxResult.success(this.dictTypeService.selectDictTypeById(dictid));
	  }

	  /**
	   * @功能描述:删除
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @DeleteMapping("deleteDictTypeByIds/{dictIds}")
	  @Log(title = "根据ID删除字典类型", businessType = BusinessType.DELETE)
	  public AjaxResult updateDictType(@PathVariable @Validated @NotEmpty(message = "要删除的类型字典ID不能为空") Long[] dictIds) {
			return AjaxResult.toAjax(this.dictTypeService.deleteDictTypeByIds(dictIds));
	  }

	  /**
	   * @功能描述:查询所有可用的字典类型
	   * @Author: TeouBle
	   * @Date: 2020/8/29 20:02
	   */
	  @GetMapping("selectAllDictType")
	  @Log(title = "查询所有可用的字典类型", businessType = BusinessType.SELECT)
	  public AjaxResult selectAllDictType() {
			return AjaxResult.success(this.dictTypeService.list().getData());
	  }


	  /**********************************************同步字典数据到redis当中缓存********************************************************/
	  /**
	   * 同步缓存
	   * 目的：
	   * 每一个类型字典都有 多个 字典数据
	   * 但是字典数据一般不会进行更改 所以我们进行缓存到redis当中
	   * 1.查询出所有可用的字典类型数据
	   * 2.在根据字典类型查询字典数据
	   * 3.把字典数据生成json存放到redis当中
	   * 设计KEY
	   * dict：dictType
	   * 如：dict:sys_user_sex---->[{},{},{}]
	   */
	  @GetMapping("dictCacheAsync")
	  @Log(title = "字典数据缓存同步到redis", businessType = BusinessType.INSERT)
	  public AjaxResult dictCacheAsync() {
			try {
				  this.dictTypeService.dictCacheAsync();
				  return AjaxResult.success();
			} catch (Exception e) {
				  System.out.println(e);
				  return AjaxResult.error();
			}
	  }

	  /**
	   * @Description: 杨不易个人网址:http://yangbuyi.top
	   * 功能描述: 清理指定缓存数据
	   * @Author: TeouBle
	   * @Date: 2020/8/31 14:07
	   */
	  @PostMapping("dictClearCacheAsync")
	  @Log(title = "字典缓存清理到redis", businessType = BusinessType.DELETE)
	  public AjaxResult dictClearCacheAsync() {
			try {
				  this.dictTypeService.dictClearCacheAsync();
				  return AjaxResult.success("字典缓存清理成功");
			} catch (Exception e) {
				  e.printStackTrace();
				  return AjaxResult.error(e.getMessage());
			}
	  }
	  /******************************************************************************************************/


}
